﻿namespace Yom.WebService.Setup
{
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.IO;
    using System.Text;
    using System.Web;
    using Yom.Common;

    public class SetupImport
    {
        public void OutWrite()
        {
            HttpResponse Response = HttpContext.Current.Response;
            JsonModel Js = new JsonModel();
            int State = 0;
            string Msg = string.Empty;

            string dbhost = Factory.Request("dbhost");
            string dbuser = Factory.Request("dbuser");
            string dbpass = Factory.Request("dbpass");
            string dbname = Factory.Request("dbname");
            string newdb = Factory.Request("NewDB");

            if (string.IsNullOrEmpty(dbhost) || string.IsNullOrEmpty(dbuser) || string.IsNullOrEmpty(dbname))
            {
                Msg = "数据库连接信息不完整, 请填写完整!";
                State = 1;
            }
            else
            {
                SqlConnection conn;
                SqlCommand cmd = new SqlCommand();

                if (newdb == "Yes")
                {
                    conn = new SqlConnection(string.Format("server={0};uid={1};pwd={2}", dbhost, dbuser, dbpass));
                    try
                    {
                        conn.Open();
                        cmd.Connection = conn;
                        cmd.CommandType = CommandType.Text;
                        cmd.CommandText = "CREATE DATABASE " + dbname;
                        cmd.ExecuteNonQuery();
                        cmd.Dispose();
                        conn.Close();
                        conn.Dispose();
                    }
                    catch (Exception ex)
                    {
                        Msg = "创建数据库出错: " + ex.Message.ToString();
                        State = 1;

                        Js.Add("Msg", Msg);
                        Js.Add("State", State);
                        Response.Write(new Json(Js));
                    }
                }
                else
                {
                    conn = new SqlConnection(string.Format("server={0};uid={1};pwd={2};database={3}", dbhost, dbuser, dbpass, dbname));
                    conn.Open();
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.Text;
                    StreamReader sr = new StreamReader(HttpContext.Current.Server.MapPath("~/Installer/Script/Clear.sql"), Encoding.UTF8);
                    string strSQL = string.Empty;
                    string tmpSQL = string.Empty;

                    while (!sr.EndOfStream)
                    {
                        tmpSQL = sr.ReadLine();

                        if (tmpSQL != "GO")
                            strSQL += tmpSQL + "\n";
                        else
                        {
                            cmd.CommandText = strSQL;
                            cmd.ExecuteNonQuery();
                            strSQL = string.Empty;
                        }
                    }
                    conn.Close();
                    conn.Dispose();
                    sr.Close();
                    sr.Dispose();
                }

                conn = new SqlConnection(string.Format("server={0};uid={1};pwd={2};database={3}", dbhost, dbuser, dbpass, dbname));

                try
                {
                    conn.Open();
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.Text;
                    StreamReader sr = new StreamReader(HttpContext.Current.Server.MapPath("~/Installer/Script/Setup.sql"), Encoding.UTF8);
                    string strSQL = string.Empty;
                    string tmpSQL = string.Empty;

                    while (!sr.EndOfStream)
                    {
                        tmpSQL = sr.ReadLine();

                        if (tmpSQL != "GO")
                            strSQL += tmpSQL + "\n";
                        else
                        {
                            cmd.CommandText = strSQL;
                            cmd.ExecuteNonQuery();
                            strSQL = string.Empty;
                        }
                    }

                    sr.Close();
                    sr.Dispose();
                }
                catch (Exception ex)
                {
                    Msg = "导入数据库错误: " + ex.Message.ToString();
                    State = 1;
                }
                finally
                {
                    conn.Close();
                    conn.Dispose();
                }
            }

            Js.Add("Msg", Msg);
            Js.Add("State", State);
            Response.Write(new Json(Js));
        }
    }
}